<?php

declare(strict_types=1);

namespace App\Controller\Backend;

use App\Controller\AbstractController;
use App\Model\CardNo;
use App\Model\Mobile;
use App\Request\Backend\CardCreateRequest;
use App\Request\Backend\CardEditRequest;
use App\Request\Backend\CardIdRequest;
use App\Request\Backend\MobileCreateRequest;
use App\Request\Backend\MobileEditRequest;
use App\Request\Backend\MobileIdRequest;
use App\Resource\BaseCollection;
use App\Resource\BaseResource;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class CardsManage extends AbstractController
{
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('card_no')) {

            $where[] = ['card_no', 'like', "{$request->input('card_no')}"];
        }

        $list = CardNo::where($where)->orderByDesc('id')->paginate();

        return new BaseCollection($list);
    }

    public function cardAdd(CardCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        CardNo::updateOrCreate(
            $data,$data
        );
        return new BaseResource();
    }

    public function cardEdit(CardEditRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        CardNo::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function cardDel($id, ResponseInterface $response)
    {
        CardNo::where('id', $id)->delete();
        return new BaseResource();
    }
}
